<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 25, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

class ToolbarControl {
	/**
	 * Toolbar array
	 *
	 * @access	private
	 * @var		array
	 */
	var $_bar = array ();
	var $_title;
	
	/**
	 * Set a value
	 *
	 * @access	public
	 * @return	string	The set value
	 */
	function appendButton($title, $alias, $resource, $icon, $attribute) {
		// Push button onto the end of the toolbar array
		$arr = array ();
		$arr ['title'] = $title;
		$arr ['resource'] = $resource;
		$arr ['icon'] = $icon;
		$arr ['attribute'] = $attribute;
		$arr ['alias'] = $alias;
		
		$this->_bar [$alias] = $arr;
		return true;
	}
	
	/**
	 * Render All Button
	 *
	 * @access	public
	 * @return	string	HTML
	 */
	function render() {
		$html = '';
		// Start toolbar div
		$html .= '<ul id="admin-toolbar">';
		
		// Render each button in the toolbar
		foreach ( $this->_bar as $button ) {
			$html .= $this->renderButton ( $button );
		}
		
		// End toolbar div
		$html .= '</ul>';
		
		return $html;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param Button $node
	 * @return unknown
	 */
	function renderButton(&$node) {
		$text = $node ['title'];
		if (! empty ( $node ['icon'] ))
			$icon = SYS_CUR_TMPL_URL . 'images/' . $node ['icon'];
		else
			$icon = SYS_CUR_TMPL_URL . 'images/generic.png';
		$icon = '<img src="' . $icon . '" border="0"/> ';
		$resource = $node ['resource'];
		$attribute = $node ['attribute'];
		if (is_object ( $resource )) {
			$link = $resource->getLink ();
		} else {
			$link = $resource;
		}
		
		$alias = $node ['alias'];
		
		$output = "<li id=\"$alias\"><a href=$link $attribute>{$icon}{$text}</a></li>";
		return $output;
	}
	
	/**
	 * Get a Button
	 *
	 * @param unknown_type $alias
	 * @return unknown
	 */
	function getButton($alias) {
		return $this->_bar [$alias];
	}
	
	/**
	 * Get All Button
	 *
	 * @return Array of All Button
	 */
	function getAllButton() {
		return $this->_bar;
	}
	
	/**
	 * Edit Button
	 *
	 * @param string $title
	 * @param string $alias
	 * @param Link/ResourceObject $resource
	 * @param image name $icon
	 * @param attribute of Link $attribute
	 * @return unknown
	 */
	function editButton($title, $alias, $resource, $icon, $attribute) {
		if (! empty ( $this->_bar [$alias] ))
			return $this->appendButton ( $title, $alias, $resource, $icon, $attribute );
		else
			return false;
	}
	
	/**
	 * Delete a Button
	 *
	 * @param Alias of Button $alias
	 * @return True|False True if delete successful
	 */
	function deleteButton($alias) {
		if (! empty ( $this->_bar [$alias] )) {
			unset ( $this->_bar [$alias] );
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Set Title of Application
	 *
	 * @param string $title
	 */
	function setTitle($title) {
		$this->_title = $title;
	}
	
	/**
	 * Append the title after the existing one. 
	 *
	 * @param string $title
	 * @param string $connector
	 */
	function appendTitle($title, $connector = ' -> ') {
		$this->_title .= "{$connector}{$title}";
	}
	
	/**
	 * Get title of Application
	 *
	 * @return string $title
	 */
	function getTitle() {
		return $this->_title;
	}
}

?>